package com.infoauto.opc.gateway.controller;

import com.infoauto.opc.gateway.entity.Result;
import com.infoauto.opc.gateway.entity.ResultCode;
import com.infoauto.opc.gateway.model.gtm4.CheckTheNumberOfUnits1;
import com.infoauto.opc.gateway.service.CheckTheNumberOfUnits1Service;
import com.sun.deploy.net.URLEncoder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 检查通过台数1
 */

@CrossOrigin
@RestController
@RequestMapping("/checkTheNumberOfUnits1")
public class CheckTheNumberOfUnits1Controller {
    @Autowired
    private CheckTheNumberOfUnits1Service service;

    @RequestMapping(value="/list" ,method= RequestMethod.GET)
    public Result selectByProducTionTimeAndShift(String productionTime,Integer shift){
        try{
            List<CheckTheNumberOfUnits1> list = service.selectByProducTionTimeAndShift(productionTime,shift);
            return new Result(ResultCode.SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
   }
    /**
     * 修改检查通过台数1
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Result update(@RequestBody CheckTheNumberOfUnits1 checkTheNumberOfUnits1) {
        //2.调用service更新
        service.update(checkTheNumberOfUnits1);
        return new Result(ResultCode.SUCCESS);
    }

    @RequestMapping(value = "/updateRemark", method = RequestMethod.PUT)
    public Result updateRemark(String id,String remark) {
        CheckTheNumberOfUnits1 checkTheNumberOfUnits1 = service.selectById(id);
        if(checkTheNumberOfUnits1!=null){
            checkTheNumberOfUnits1.setRemark(remark);
            checkTheNumberOfUnits1.setModifyDateTime(new Date());
            //2.调用service更新
            service.update(checkTheNumberOfUnits1);
        }
        return new Result(ResultCode.SUCCESS);
    }

    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public void export(HttpServletResponse response,String productionTime,Integer shift) throws Exception {
        //1.构造数据
        List<CheckTheNumberOfUnits1> list = service.selectByProducTionTimeAndShift(productionTime,shift);
        //2.创建工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        String[] head1 = {"GTMC#4"};
        //3.构造sheet
        Sheet sheet = workbook.createSheet("检查通过台数1");
        //创建单元格样式对象
        CellStyle cellStyle = workbook.createCellStyle();
        //设置居中显示
        cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        cellStyle.setBorderBottom(BorderStyle.MEDIUM);//下边框
        cellStyle.setBorderTop(BorderStyle.MEDIUM);//上边框
        cellStyle.setBorderLeft(BorderStyle.MEDIUM);//左边框
        cellStyle.setBorderRight(BorderStyle.MEDIUM);//有边框
        //设置单元格样式
        //设置边框
        //创建单元格样式对象
        CellStyle cellStyle1 = workbook.createCellStyle();
        cellStyle1.setBorderBottom(BorderStyle.NONE);//下边框
        cellStyle1.setBorderTop(BorderStyle.NONE);//上边框
        //设置字体
        Font font = workbook.createFont();//创建字体对象
        font.setFontName("宋体");//设置字体
        font.setFontHeightInPoints((short)16);//设置字号
        cellStyle1.setFont(font);
        Row row =sheet.createRow(0);
        AtomicInteger headersAi = new AtomicInteger();
        for (String title : head1) {
            Cell cell = row.createCell(headersAi.getAndIncrement());
            cell.setCellValue(title);
            cell.setCellStyle(cellStyle1);
        }
        //合并单元格
        CellRangeAddress region1 =new CellRangeAddress(0, 0, 0, 12);
        sheet.addMergedRegion(region1);
        AtomicInteger ai1 = new AtomicInteger(1);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Cell cell = null;
        Row row2 = sheet.createRow(ai1.getAndIncrement());
        cell = row2.createCell(0);
        cell.setCellValue("Date");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row2.createCell(1);
        cell.setCellValue(productionTime);
        //设置样式
        cell.setCellStyle(cellStyle);
        sheet.setColumnWidth(1, 12 * 256);//设置第一列的宽度是31个字符宽度
        sheet.setColumnWidth(12, 30 * 256);//设置第一列的宽度是31个字符宽度
        row2.setHeightInPoints(22);//设置行的高度是50个点
        AtomicInteger ai2 = new AtomicInteger(2);
        Row row3 = sheet.createRow(ai2.getAndIncrement());
        cell = row3.createCell(0);
        cell.setCellValue("Shift");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row3.createCell(1);
        cell.setCellValue(shift);
        //设置样式
        cell.setCellStyle(cellStyle);
        //合并单元格
        CellRangeAddress region2 =new CellRangeAddress(1, 2, 2, 12);
        sheet.addMergedRegion(region2);
        AtomicInteger ai3 = new AtomicInteger(3);
        Row row4 = sheet.createRow(ai3.getAndIncrement());
        cell = row4.createCell(0);
        cell.setCellValue("编号");
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(1);
        cell.setCellStyle(cellStyle);
        cell.setCellValue("时间带");
        cell = row4.createCell(2);
        cell.setCellValue("检查通台数");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(6);
        cell.setCellValue("合格台数");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(9);
        cell.setCellValue("不合格台数");
        //设置样式
        cell.setCellStyle(cellStyle);
        cell = row4.createCell(12);
        cell.setCellValue("特记事项");
        cell.setCellStyle(cellStyle);
        CellRangeAddress region5 =new CellRangeAddress(3, 3, 2, 5);
        //合并单元格
        sheet.addMergedRegion(region5);
        setCellBorder(sheet, region5);
        CellRangeAddress region6 =new CellRangeAddress(3, 3, 6, 8);
        //合并单元格
        sheet.addMergedRegion(region6);
        setCellBorder(sheet, region6);

        CellRangeAddress region7 =new CellRangeAddress(3, 3, 9, 11);
        //合并单元格
        sheet.addMergedRegion(region7);
        setCellBorder(sheet, region7);
        CellRangeAddress region9 =new CellRangeAddress(3, 4, 0, 0);
        //合并单元格
        sheet.addMergedRegion(region9);
        setCellBorder(sheet, region9);
        CellRangeAddress region10 =new CellRangeAddress(3, 4, 1, 1);
        //合并单元格
        sheet.addMergedRegion(region10);
        setCellBorder(sheet, region10);
        CellRangeAddress region11 =new CellRangeAddress(3, 4, 12, 12);
        //合并单元格
        sheet.addMergedRegion(region11);
        setCellBorder(sheet, region11);
        //3.构造sheet
        String[] titles = {"编号", "时间带", "计划","实际", "实际累积","可动率","纯直行","擦摩","积累","再涂装","废弃","积累","特记事项"};
        Row row5 =sheet.createRow(4);
        AtomicInteger ai4 = new AtomicInteger();
        for (String title : titles) {
            cell = row5.createCell(ai4.getAndIncrement());
            cell.setCellValue(title);
            //设置样式
            cell.setCellStyle(cellStyle);
        }
        AtomicInteger datasAi = new AtomicInteger(5);
        int i = 1;
        Integer passPlan =0 ;//通过计划
        Integer throughActual = 0;//通过实际
        Integer throughActualAccumulation = 0;//通过实际累积
        Integer passMobility = 0;//通过可动率
        Integer qualifiedPureStraight = 0;//合格纯直行
        Integer wipeTheQualified = 0;//合格擦摩
        Integer accumulationOfQualified = 0;//合格积累
        Integer unqualifiedReapply = 0;//不合格再涂装
        Integer unqualifiedAbandonment = 0;//不合格废弃
        Integer nonconformingAccumulation = 0;//不合格积累
        if(list.size()>0){
            for (CheckTheNumberOfUnits1 report : list) {
                passPlan = passPlan+report.getPassPlan();
                throughActual = throughActual+report.getThroughActual();
                throughActualAccumulation = throughActualAccumulation + report.getThroughActualAccumulation();
                passMobility = passMobility + report.getPassMobility();
                qualifiedPureStraight = qualifiedPureStraight+report.getQualifiedPureStraight();
                wipeTheQualified = wipeTheQualified+report.getWipeTheQualified();
                accumulationOfQualified = accumulationOfQualified + report.getAccumulationOfQualified();
                unqualifiedReapply = unqualifiedReapply + report.getUnqualifiedReapply();
                unqualifiedAbandonment = unqualifiedAbandonment + report.getUnqualifiedAbandonment();
                nonconformingAccumulation = nonconformingAccumulation+report.getNonconformingAccumulation();
                Row dataRow = sheet.createRow(datasAi.getAndIncrement());
                //编号
                cell = dataRow.createCell(0);
                cell.setCellValue(i++);
                cell.setCellStyle(cellStyle);
                //时间带
                cell = dataRow.createCell(1);
                cell.setCellValue(report.getTimeZone());
                cell.setCellStyle(cellStyle);
                //通过计划
                cell = dataRow.createCell(2);
                cell.setCellValue(report.getPassPlan());
                cell.setCellStyle(cellStyle);
                //通过实际
                cell = dataRow.createCell(3);
                cell.setCellValue(report.getThroughActual());
                cell.setCellStyle(cellStyle);
                //通过实际累积
                cell = dataRow.createCell(4);
                cell.setCellValue(report.getThroughActualAccumulation());
                cell.setCellStyle(cellStyle);
                //通过可动率
                cell = dataRow.createCell(5);
                cell.setCellValue(report.getPassMobility());
                cell.setCellStyle(cellStyle);
                //合格纯直行
                cell = dataRow.createCell(6);
                cell.setCellValue(report.getQualifiedPureStraight());
                cell.setCellStyle(cellStyle);
                //合格擦磨
                cell = dataRow.createCell(7);
                cell.setCellValue(report.getWipeTheQualified());
                cell.setCellStyle(cellStyle);
                //合格积累
                cell = dataRow.createCell(8);
                cell.setCellValue(report.getAccumulationOfQualified());
                cell.setCellStyle(cellStyle);
                //不合格再涂装
                cell = dataRow.createCell(9);
                cell.setCellValue(report.getUnqualifiedReapply());
                cell.setCellStyle(cellStyle);
                //合格废弃
                cell = dataRow.createCell(10);
                cell.setCellValue(report.getUnqualifiedAbandonment());
                cell.setCellStyle(cellStyle);
                //不和格积累
                cell = dataRow.createCell(11);
                cell.setCellValue(report.getNonconformingAccumulation());
                cell.setCellStyle(cellStyle);
                //特记事项
                cell = dataRow.createCell(12);
                cell.setCellValue(report.getRemark());
                cell.setCellStyle(cellStyle);
            }
            AtomicInteger ai6 = new AtomicInteger(list.size()+5);
            Row row6 = sheet.createRow(ai6.getAndIncrement());
            cell = row6.createCell(0);
            cell.setCellValue(" TOTAL");
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(1);
            cell.setCellValue(0);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(2);
            cell.setCellValue(passPlan);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(3);
            cell.setCellValue(throughActual);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(4);
            cell.setCellValue(throughActualAccumulation);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(5);
            cell.setCellValue(passMobility);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(6);
            cell.setCellValue(qualifiedPureStraight);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(7);
            cell.setCellValue(wipeTheQualified);
            cell.setCellStyle(cellStyle);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(8);
            cell.setCellValue(accumulationOfQualified);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(9);
            cell.setCellValue(unqualifiedReapply);
            cell.setCellStyle(cellStyle);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(10);
            cell.setCellValue(unqualifiedAbandonment);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(11);
            cell.setCellValue(nonconformingAccumulation);
            cell.setCellStyle(cellStyle);
            cell = row6.createCell(12);
            cell.setCellValue("");
            cell.setCellStyle(cellStyle);
            CellRangeAddress region8 =new CellRangeAddress(list.size()+5, list.size()+5, 0, 1);
            //合并单元格
            sheet.addMergedRegion(region8);
        }

        String fileName = URLEncoder.encode(sheet.getSheetName()+".xlsx", "UTF-8");
        response.setContentType("application/octet-stream");
        response.setHeader("content-disposition", "attachment;filename=" + new
                String(fileName.getBytes("ISO8859-1")));
        response.setHeader("filename", fileName);
        workbook.write(response.getOutputStream());
    }
    //合并单元格样式
    private void setCellBorder(Sheet sheet, CellRangeAddress cellAddresses) {
        RegionUtil.setBorderTop(BorderStyle.MEDIUM, cellAddresses, sheet);
        RegionUtil.setBorderBottom(BorderStyle.MEDIUM, cellAddresses, sheet);
        RegionUtil.setBorderLeft(BorderStyle.MEDIUM, cellAddresses, sheet);
        RegionUtil.setBorderRight(BorderStyle.MEDIUM, cellAddresses, sheet);
    }



}
